import { Filds } from '@/common/type';
import {
  queryPage, queryInfoById, create, update, remove,
  queryTransform,
  queryBrand
} from '@/pages/data/device_fire_host/service';

import { queryDictList } from '@/services/dict';
import { routerRedux } from 'dva/router';
import { DICT, BRAND } from '@/pages/data/device_fire_host/enum';
import { message } from 'antd';

export default {
  namespace: 'device_fire_host',

  state: {
    list: { data: [] },
    details: [],
    cond: {},
    dict: {},
    brand: [],
    location: [],
  },

  effects: {
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: {} });
      yield put({ type: 'queryPage' })
    },
    *updateCond({ payload }, { put }) {
      yield put({ type: 'setCond', payload });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1, pageSize: 10 } }, { call, put, select }) {
      const cond = yield select(state => state.device_fire_host.cond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      yield put({ type: 'setPage', payload: { list, total: +total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryEditInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.id);
      const items: Filds = {};
      payload.keys.forEach((item: string) => {
        items[item] = response[item];
      });
      yield put({ type: 'setEditInfo', payload: items })
    },
    *create({ payload }, { call, put }) {
      const ans = yield call(create, payload);
      if (ans) {
        message.success(ans);
        yield put(routerRedux.goBack());
      }
    },
    *queryBrand({ payload }, { call, put, select }) {
      const { option = [], target } = payload || {};
      const len = option.length;
      const response = yield call(queryBrand, {
        tail: BRAND[len].tail,
        ...(BRAND[len].params || {}),
        ...(len > 0 ? { [`${BRAND[len - 1].type}Id`]: target.id } : {})
      });

      const brand = yield select(state => state.device_fire_host.brand);
      const ans = Array.isArray(response) ? response.map((v, ind) => ({
        label: v[BRAND[len].name],
        value: `${v.id}%%${ind}`,
        isLeaf: !(len < 1),
      })) : [];

      let it = brand
      option.forEach(v => {
        it = it[v].children ? it[v].children : it[v]
      })

      it.children = ans;
      it.loading = false;
      yield put({ type: 'setBrand', payload: brand.length === 0 ? ans : brand })
    },
    *queryType({ payload }, { call, put, select }) {
      const { option = [], target } = payload || {};
      const len = option.length;
      const response = yield call(queryBrand, {
        tail: BRAND[len].tail,
        ...(BRAND[len].params || {}),
        ...(len > 0 ? { [`${BRAND[len - 1].type}Id`]: target.id } : {})
      });

      const brand = yield select(state => state.device_fire_host.brand);
      const ans = Array.isArray(response) ? response.map((v, ind) => ({
        label: v[BRAND[len].name],
        value: `${v.id}%%${ind}`,
        isLeaf: !(len < 1),
      })) : [];

      let it = brand
      option.forEach(v => {
        it = it[v].children ? it[v].children : it[v]
      })

      it.children = ans;
      it.loading = false;
      yield put({ type: 'setBrand', payload: brand.length === 0 ? ans : brand })
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.goBack());
    },
    *remove({ payload }, { call, put }) {
      yield call(remove, payload);
      yield put({ type: 'queryPage' });
    },
    *queryTransform(_, { call, put }) {
      const { list } = yield call(queryTransform);
      yield put({ type: 'setTransform', payload: Array.isArray(list) ? list : [] })
    },
    *init({ payload }, { put }) {
      yield put({ type: 'upload/setImgValue', payload: { url: payload.url } });
      yield put({ type: 'queryDict', payload: payload.dict });
      yield put({ type: 'queryTransform' });
      yield put({ type: 'queryBrand' });
    },
    
  },

  reducers: {
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    },
    setBrand(state, action) {
      return {
        ...state,
        brand: action.payload
      }
    },
    setType(state, action) {
      return {
        ...state,
        type: action.payload
      }
    },
    setTransform(state, action) {
      return {
        ...state,
        transform: action.payload
      }
    },
  }
}